package com.xju.admin.controller;

import com.xju.admin.entity.Dictionary;
import com.xju.admin.entity.User;
import com.xju.admin.entity.UserResult;
import com.xju.admin.entity.UserShow;
import com.xju.admin.service.DictionaryService;
import com.xju.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DictionaryService dictionaryService;

    @RequestMapping(value = {"/", "user-list"})
    public String userList(Model model) {
        UserResult userResult = userService.findAll();
        model.addAttribute("userResult", userResult);
        return "module_one/user_list";
    }

    @RequestMapping("user-modify")
    public String userModify(@RequestParam(value = "userId") String id,
                             @RequestParam(value = "userName") String name,
                             @RequestParam(value = "userEmail") String email,
                             @RequestParam(value = "userPassword") String password,
                             @RequestParam(value = "userStatus") String status,
                             @RequestParam(value = "userProfile") String profile) {
        userService.modify(id,name,email,password,status,profile);

        return "redirect:/user-list";
    }

    @RequestMapping("user-delete")
    @ResponseBody
    public Object userDelete(String id) {
        userService.delete(id);
        Map<String, Object> map = new HashMap<>();
        map.put("ok", true);
        return map;
    }

    @RequestMapping("user-add-page")
    public String userAddPage(Model model){
        List<Dictionary> dictionaryList=dictionaryService.findByType("用户状态");
        model.addAttribute("userStatus",dictionaryList);
        return "module_one/user_add";
    }

    @RequestMapping("user-add")
    public String userAdd(@RequestParam(value = "userName") String name,
                          @RequestParam(value = "userStatus") String status,
                          @RequestParam(value = "userEmail") String email,
                          @RequestParam(value = "userPassword") String password,
                          @RequestParam(value = "userProfile") String userProfile){
        userService.addUser(name,status,email,password,userProfile);
        return "redirect:/user-list";
    }

    @RequestMapping("user-profile")
    public String profile(Model model){
        User user=userService.findByName("admin");
        model.addAttribute("user",user);
        return "module_one/profile";
    }

    @RequestMapping("save-profile")
    public String saveProfile(String userId,String username,String password,String profile){
        userService.saveProfile(userId,username,password,profile);
        if (password!=null){
            return "redirect:/login";
        }
        return "redirect:/user-profile";
    }
}
